package com.itany.mall.action;

import java.util.List;
import java.util.Scanner;

import com.itany.mall.entity.Product;
import com.itany.mall.entity.ProductType;
import com.itany.mall.exception.ProductNameExistsException;
import com.itany.mall.exception.ProductTypeNameExistsException;
import com.itany.mall.exception.ProductTypeNotFoundException;
import com.itany.mall.factory.Autowired;
import com.itany.mall.factory.ObjectFactory;
import com.itany.mall.service.ProductService;
import com.itany.mall.service.ProductTypeService;

public class AdminAction {

	@Autowired
	private ProductTypeService productTypeService;

	@Autowired
	private ProductService productService;

	{
		ObjectFactory.autowired(this);
	}

	public void menu() {
		do {
			System.out.println("1. 商品类型管理");
			System.out.println("2. 商品管理");
			System.out.println("3. 退出登录");
			System.out.println("请选择");
			switch (new Scanner(System.in).nextLine()) {
				case "1":
					productTypeMenu();
					break;
				case "2":
					productMenu();
					break;
				case "3":
					return;
				default:
					System.out.println("选择错误");
			}
		} while (true);
	}

	private void productMenu() {
		do {
			System.out.println("1. 查看所有商品");
			System.out.println("2. 添加商品");
			System.out.println("3. 返回上级");
			switch (new Scanner(System.in).nextLine()) {
				case "1":
					showAllProduct();
					break;
				case "2":
					addProduct();
					break;
				case "3":
					return;
				default:
					System.out.println("选择错误");
			}
		} while (true);
	}

	private void addProduct() {
		System.out.println("请输入商品名称");
		String name = new Scanner(System.in).nextLine();
		if (name == null || name.trim().isEmpty()) {
			System.out.println("商品名称不可为空");
			return;

		}
		name = name.trim();

		System.out.println("请输入商品单价");
		String priceStr = new Scanner(System.in).nextLine();
		if (priceStr == null || !priceStr.matches("[\\d]+([\\.][\\d]{1,2})?")) {
			System.out.println("请输入有效价格");
			return;
		}
		double price = Double.parseDouble(priceStr);

		System.out.println("请选择类型");
		showAllProductTypes();
		String typeIdStr = new Scanner(System.in).nextLine();
		if (typeIdStr == null || !typeIdStr.matches("[\\d]+")) {
			System.out.println("请输入有效类型编号");
			return;
		}
		long typeId = Long.parseLong(typeIdStr);

		try {
			productService.addProduct(name, price, typeId);
			System.out.println("添加成功");
		} catch (ProductNameExistsException e) {
			System.out.println("商品名称已存在");
		} catch (ProductTypeNotFoundException e) {
			System.out.println("所选商品类型不存在");
		}

	}

	private void showAllProduct() {
		List<Product> list = productService.findAll();
		for (Product p : list) {
			System.out.print("商品编号: " + p.getId() + ", ");
			System.out.print("商品名称: " + p.getName() + ", ");
			System.out.print("商品单价: " + p.getPrice() + ", ");
			System.out.println("商品类型: " + p.getType().getName());
		}
	}

	private void productTypeMenu() {
		do {
			System.out.println("1. 查看所有商品类型");
			System.out.println("2. 添加商品类型");
			System.out.println("3. 返回上级");
			switch (new Scanner(System.in).nextLine()) {
				case "1":
					showAllProductTypes();
					break;
				case "2":
					addProductType();
					break;
				case "3":
					return;
				default:
					System.out.println("选择错误");
			}
		} while (true);
	}

	// 添加商品类型
	private void addProductType() {
		System.out.println("请输入商品类型名称");
		String name = new Scanner(System.in).nextLine();
		if (name == null || name.trim().isEmpty()) {
			System.out.println("商品类型名称不可为空");
			return;
		}
		try {
			productTypeService.addProductType(name.trim());
			System.out.println("添加成功");
		} catch (ProductTypeNameExistsException e) {
			System.out.println("商品类型名称已存在");
		}
	}

	// 查看所有商品类型
	private void showAllProductTypes() {
		List<ProductType> list = productTypeService.findAll();
		for (ProductType pt : list) {
			System.out.print("类型编号: " + pt.getId() + ", ");
			System.out.println("类型名称: " + pt.getName());
		}
	}

}
